package com.lei.session.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Optional;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.Out;

import com.lei.session.constant.UserConstant;
import com.lei.session.entity.Permission;
import com.lei.session.util.PremissionUtil;
import com.lei.sqlGoodsJsp.Constant;
import com.lei.sqlJsp.entity.GoodsEntity;
import com.lei.sqlJsp.entity.Page;
import com.lei.sqlJsp.service.GoodsService;
import com.lei.sqlJsp.service.Impl.GoodsServiceImpl;

public class ToolsServlet extends HttpServlet {
	static GoodsService goodsService = new GoodsServiceImpl();
	private Long count = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		System.out.println(method);
		//获取用户权限
		List<Permission> premissionList = (List<Permission>)req.getSession().getAttribute(UserConstant.USER_PRESSION);
		if ("add".equals(method)) {
			if(!PremissionUtil.checkPremission(premissionList, "add")) {
				resp.sendRedirect("/noPremission.jsp");
				return;
			}
			add(req, resp);
			resirectList(resp);
		} else if ("delete".equals(method)) {
			if(!PremissionUtil.checkPremission(premissionList, "delete")) {
				resp.sendRedirect("/noPremission.jsp");
				return;
			}
			delete(req);
			resirectList(resp);
		} else if ("toUpdate".equals(method)) {
			toUpdate(req, resp);
		} else if ("update".equals(method)) {
			edit(req, resp);
			resirectList(resp);
		} else if ("list".equals(method)) {
			list(req, resp);
		} else {
			resirectList(resp);
		}
	}

	private void resirectList(HttpServletResponse res) throws IOException {
		res.sendRedirect("GoodsServlet?method=list");
	}

	private void add(HttpServletRequest req, ServletResponse res) throws ServletException, IOException {
		String shopNumber = req.getParameter("shopNumber");
		String shopName = req.getParameter("shopName");
		String deleteLogo = req.getParameter("deleteLogo");
		String creator =req.getParameter("creator");
		
		GoodsEntity goods = new GoodsEntity();
		goods.setShopNumber(shopNumber);
		goods.setShopName(shopName);
		goods.setDeleteLogo(deleteLogo);
		goods.setCreator((String)req.getSession().getAttribute(Constant.USER_INFO));
		goods.setMender((String)req.getSession().getAttribute(Constant.USER_INFO));
//		add(Goods)方法未为类型Goods定义
		goodsService.add(goods);
	}

	private void delete(ServletRequest req) {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println("id");
		goodsService.delete(id);
	}

	private void edit(HttpServletRequest req, ServletResponse res) throws ServletException, IOException {
		Long id = Long.valueOf(req.getParameter("id"));
		String shopNumber = req.getParameter("shopNumber");
		String shopName = req.getParameter("shopName");
		String deleteLogo = req.getParameter("deleteLogo");
		GoodsEntity goods = new GoodsEntity();
		goods.setId(id);
		goods.setShopName(shopName);
		goods.setShopName(shopName);
		goods.setDeleteLogo(deleteLogo);
		goods.setCreator((String)req.getSession().getAttribute(Constant.USER_INFO));
		goods.setMender((String)req.getSession().getAttribute(Constant.USER_INFO));
		goodsService.update(goods);
	}

	private void toUpdate(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println("id");
		PrintWriter out = res.getWriter();
		req.setAttribute("goods", goodsService.getById(id));
		req.getRequestDispatcher("newSql/edit.jsp").forward(req, res);
	}

	private void list(HttpServletRequest req, ServletResponse resp) throws ServletException, IOException {
		req.setAttribute("goodsOne", ShopPage(req));
		req.getRequestDispatcher("newSql/list.jsp").forward(req, resp);
	}

	private Page<GoodsEntity> ShopPage(HttpServletRequest req) throws ServletException, IOException {

		String currentPage = req.getParameter("currentPage");
		int intCurrentPage = Optional.ofNullable(currentPage).map(e -> Integer.parseInt(currentPage)).orElse(1);
		int pageSize = 3;
		return goodsService.page(intCurrentPage, pageSize);
		
	}
}
